<template>
  <div class="register-container">
    <div class="panel-left"></div>
    <div class="panel-right">
      <h2>创建您的账户</h2>
      <p>加入我们，开始您的旅程!</p>
      <el-form
        ref="formRef"
        :model="form"
        :rules="rules"
        class="register-form"
      >
        <el-form-item prop="username">
          <el-input v-model="form.username" placeholder="用户名" />
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="form.password" type="password" placeholder="密码" show-password />
        </el-form-item>
        <el-form-item prop="confirmPassword">
          <el-input v-model="form.confirmPassword" type="password" placeholder="请再次输入密码" show-password />
        </el-form-item>
        <el-form-item class="form-actions">
          <el-button class="sign-btn" type="primary" @click="handleRegister">注册</el-button>
        </el-form-item>
        <div class="login-link">
          <span>已有账户? </span><a href="#" @click.prevent="goToLogin">返回登录</a>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { register } from '@/api/user'
import type { ApiResponse } from '@/types/api'

const router = useRouter()
const formRef = ref()

const form = reactive({
  username: '',
  password: '',
  confirmPassword: ''
})

const validatePass = (rule: any, value: string, callback: any) => {
  if (value === '') {
    callback(new Error('请输入密码'))
  } else {
    if (form.confirmPassword !== '') {
      formRef.value?.validateField('confirmPassword')
    }
    callback()
  }
}

const validatePass2 = (rule: any, value: string, callback: any) => {
  if (value === '') {
    callback(new Error('请再次输入密码'))
  } else if (value !== form.password) {
    callback(new Error('两次输入密码不一致!'))
  } else {
    callback()
  }
}

const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, validator: validatePass, trigger: 'blur' },
    { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, validator: validatePass2, trigger: 'blur' }
  ]
}

const handleRegister = async () => {
  if (!formRef.value) return

  try {
    await formRef.value.validate()
    const res = await register({
      username: form.username,
      password: form.password
    })
    if (res.code === 200) {
      ElMessage.success(res.data || '注册成功')
      router.push('/login')
    } else {
      ElMessage.error(res.msg || '注册失败')
    }
  } catch (error: any) {
    ElMessage.error(error.response?.data?.msg || '注册失败')
  }
}

const goToLogin = () => {
  router.push('/login')
}
</script>

<style scoped>
@import url('https://fonts.googleapis.com/css2?family=Quicksand&display=swap');

.register-container {
  display: flex;
  width: 100vw;
  height: 100vh;
  overflow: hidden;
}

.panel-left {
  flex: 0 0 60%;
  background-image: url('/Login.png');
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
}

.panel-right {
  flex: 0 0 40%;
  background-color: white;
  padding: 40px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

.panel-right h2 {
  font-family: 'Quicksand', sans-serif;
  font-size: 2.2rem;
  font-weight: 700;
  margin-bottom: 10px;
  color: #333;
}

.panel-right p {
  color: #777;
  font-size: 1.1rem;
  margin-bottom: 30px;
}

.register-form {
  width: 100%;
  max-width: 300px;
}

.register-form :deep(.el-input__wrapper) {
  background: #f5f5f5;
  border-radius: 8px;
  box-shadow: none;
  margin-bottom: 15px;
}

.register-form :deep(.el-input__inner) {
  font-size: 1rem;
  padding: 12px 15px;
}

.form-actions {
  margin-bottom: 10px;
}

.sign-btn {
  width: 100%;
  background: #466fff;
  color: #fff;
  font-weight: 700;
  border-radius: 8px;
  padding: 14px 0;
  font-size: 1.1rem;
  box-shadow: 0 4px 10px rgba(70, 111, 255, 0.2);
  transition: all 0.2s;
  border: none;
}

.sign-btn:hover {
  background: #3355cc;
  transform: translateY(-2px);
  box-shadow: 0 8px 15px rgba(70, 111, 255, 0.25);
}

.login-link {
  width: 100%;
  max-width: 300px;
  text-align: center;
  margin-top: 20px;
}

.login-link a {
  color: #466fff;
  text-decoration: none;
  font-size: 0.9rem;
}

.login-link a:hover {
  text-decoration: underline;
}
</style>